package com.yang.controller;

import com.yang.pojo.EachStudent;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;


import javax.servlet.ServletContext;
import javax.servlet.http.HttpSession;
import java.util.*;

@Controller
public class AttrController {
    @RequestMapping("/attr")
    public String doAttr(Model model){
        model.addAttribute("myaction","/user/login");
        model.addAttribute("mytext","请登录");
        model.addAttribute("selected",true);
        model.addAttribute("unselect",false);
        return "attr";
    }

    @RequestMapping("/body")
    public String doText(Model model){
        model.addAttribute("msg","<b>java</b>");
        model.addAttribute("context","<b>vue</>,<b>mysql</b>");
        return "body";
    }

    @RequestMapping("/body/each")
    public String doEach(Model model){
        List<EachStudent> list = new ArrayList<>();
        list.add(new EachStudent(1001,"刘备",30));
        list.add(new EachStudent(1002,"张飞",25));
        list.add(new EachStudent(1003,"关羽",23));
        list.add(new EachStudent(1004,"黄忠",21));
        list.add(new EachStudent(1005,"马超",28));
        model.addAttribute("eachStudent",list);

        //创建一个map
        Map<String, String> stumap = new HashMap<>();
        stumap.put("id","1001");
        stumap.put("name","张三");
        stumap.put("age","20");
        stumap.put("班级","java一班");
        model.addAttribute("stumap",stumap);

        //list-map
        ArrayList<Map<String,EachStudent>> listMap = new ArrayList<>();
        EachStudent s1 = new EachStudent(1001,"刘备",30);
        EachStudent s2 = new EachStudent(1002,"张飞",25);
        Map<String, EachStudent> map = new HashMap<>();
        map.put("stu-1-1",s1);
        map.put("stu-1-2",s2);
        listMap.add(map);

        EachStudent s3 = new EachStudent(1003,"刘备",33);
        EachStudent s4 = new EachStudent(1004,"张飞",45);
        Map<String, EachStudent> map2 = new HashMap<>();
        map2.put("stu-2-1",s3);
        map2.put("stu-2-2",s4);
        listMap.add(map2);
        model.addAttribute("listMap",listMap);

        //普通数组
        String names[]=new String[]{"liub","sdc","kfjf"};
        model.addAttribute("names",names);

        //select
        Map<String, String> citys = new HashMap<>();
        citys.put("010","上海");
        citys.put("021","杭州");
        citys.put("032","北京");
        model.addAttribute("citys",citys);
        model.addAttribute("selected","杭州");
        return "each";
    }

    //判断if
    @RequestMapping("/body/if")
    public String doIf(Model model){
        //true
        model.addAttribute("old",true);
        model.addAttribute("login","login");
        model.addAttribute("num1",10);
        model.addAttribute("num2",-2);
        model.addAttribute("obj1",new EachStudent());
        model.addAttribute("str0","");

        //false
        model.addAttribute("yong",false);
        model.addAttribute("str1","off");
        model.addAttribute("str2","no");
        model.addAttribute("str3","false");
        model.addAttribute("num3",0);
        model.addAttribute("obj2",null);
        return "if";
    }
    @RequestMapping("/body/tpl")
    public String doTpl(Model model){
        model.addAttribute("tplname","menu");
        return "frag/main";
//        return "frag/tpl";
//        return "frag/rem";
    }

    @RequestMapping("/body/inline")
    public String doInline(Model model){
        model.addAttribute("tplname","menu");
        List<EachStudent> students = new ArrayList<>();
        EachStudent s1 = new EachStudent(1001,"刘备",30);
        EachStudent s2 = new EachStudent(1002,"张飞",25);
        EachStudent s3 = new EachStudent(1003,"关羽",26);
        students.add(s1);
        students.add(s2);
        students.add(s3);

        model.addAttribute("students",students);
        model.addAttribute("name","三国演义");
        model.addAttribute("info","我爱<b>中国</b>");
        return "inline";
    }
    @GetMapping("/body/var")
    public String doVar(Model model){
        model.addAttribute("age",20);
        return "var";
    }

    @GetMapping("/body/toolsobject")
    public String doToolsObject(Model model){
        model.addAttribute("myname","zhangsan");
        model.addAttribute("mydate",new Date());
        model.addAttribute("price",89.35);
        return "toolsobject";
    }

    @GetMapping("/body/inobject")
    public String doInObject(Model model, HttpSession session){
        model.addAttribute("attrName","我是request作用域中的数据AttrValue");
        session.setAttribute("sessionAttr","我是session中的数据");
        ServletContext context = session.getServletContext();
        context.setAttribute("contextAttr","我是ServletContext中的数据");
        return "inobject";
    }
}
